﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SWS.PIP.DM
{
	public class DataQueryService
	{
		/// <summary>
		/// 从数据库获取指定oid小票的加工数据
		/// </summary>
		/// <param name="sSpoolOid">小票OID</param>
		/// <param name="dicPartOIDtoTAG">部件OID2TAG，包括弯管Bend2TAG</param>
		/// <param name="dicWeldOIDtoTAG">焊缝OID2TAG</param>
		/// <param name="sSpoolsMat">小票材料字符串</param>
		/// <param name="sSpoolsWeld">小票焊缝字符串</param>
		/// <param name="sSpoolsAssembly">小票装配数据字符串</param>
		/// <param name="sSpoolsBending">小票弯管数据字符串</param>
		/// <param name="sSpoolsChecking">小票校管数据字符串</param>
		/// <param name="sSpoolsStartEnd">小票首尾信息字符串</param>
		/// <param name="sSpoolsDrawingProps">小票图面属性字符串</param>
		public static void AchieveData(string sSpoolOid, 
			ref Dictionary<string, string> dicPartOIDtoTAG,
			ref Dictionary<string, string> dicWeldOIDtoTAG,
			ref string sSpoolsMat,
			ref string sSpoolsWeld,
			ref string sSpoolsAssembly,
			ref string sSpoolsBending,
			ref string sSpoolsChecking,
			ref string sSpoolsStartEnd,
			ref string sSpoolsDrawingProps)
		{
			using (SMgntEntities dbEntities = new SMgntEntities())
			{
				var result = dbEntities.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
				if (result.Count() != 0)
				{
					SMgnt_SpoolInfo oSMgntSpool = result.First();

					foreach (var oSMgntPart in oSMgntSpool.SMgnt_PipePart)
					{
						dicPartOIDtoTAG.Add(oSMgntPart.Part_oid, oSMgntPart.Tag);
					}
					foreach (var oSMgntBend in oSMgntSpool.SMgnt_PipeBend)
					{
						dicPartOIDtoTAG.Add(oSMgntBend.TurnFeature_oid, oSMgntBend.Tag);
					}

					foreach (var oSMgntWeld in oSMgntSpool.SMgnt_PipeWeld)
					{
						dicWeldOIDtoTAG.Add(oSMgntWeld.Weld_oid, oSMgntWeld.Tag);
					}

					sSpoolsMat = oSMgntSpool.StringPipePartInfo;
					sSpoolsWeld = oSMgntSpool.StringPipeWeldInfo;
					sSpoolsAssembly = oSMgntSpool.StringAssemblyInfo;
					sSpoolsBending = oSMgntSpool.StringPipeBendInfo;
					sSpoolsChecking = oSMgntSpool.StringPipeCheckInfo;
					sSpoolsStartEnd = oSMgntSpool.StringStartEndInfo;
					sSpoolsDrawingProps = oSMgntSpool.StringPipeSpoolInfo;
				}
			}
		}

		public static void AchieveData(string sSpoolOid,
			ref Dictionary<string, string> dicPartOIDtoTAG,
			ref Dictionary<string, string> dicWeldOIDtoTAG,
			ref Dictionary<string, string> dicSpoolsMat,
			ref Dictionary<string, string> dicSpoolsWeld,
			ref Dictionary<string, string> dicSpoolsAssembly,
			ref Dictionary<string, string> dicSpoolsBending,
			ref Dictionary<string, string> dicSpoolsChecking,
			ref Dictionary<string, string> dicSpoolsStartEnd,
			ref Dictionary<string, string> dicSpoolsDrawingProps)
		{
			//查找同材料节点的小票
			using (SMgntEntities dbEntities = new SMgntEntities())
			{
				var result = dbEntities.SMgnt_SpoolInfo.Where(i => i.Spool_oid == sSpoolOid);
				if (result.Count() == 0)
					return;

				SMgnt_SpoolInfo oSMgntSpool = result.First();
				string sZone = oSMgntSpool.Zone;
				string sBlock = oSMgntSpool.Block;
				string sMat = oSMgntSpool.ParentAss;

				//查询节点下所有oid
				var listOfSpoolOid = dbEntities.SMgnt_SpoolInfo.Where(i => i.Zone == sZone && i.Block == sBlock && i.ParentAss == sMat).Select(i => i.Spool_oid).ToList();
				foreach (var sOid in listOfSpoolOid)
				{
					string sSpoolsMat = "";
					string sSpoolsWeld = "";
					string sSpoolsAssembly = "";
					string sSpoolsBending = "";
					string sSpoolsChecking = "";
					string sSpoolsStartEnd = "";
					string sSpoolsDrawingProps = "";
					AchieveData(sOid, ref dicPartOIDtoTAG, ref dicWeldOIDtoTAG, ref sSpoolsMat, ref sSpoolsWeld, ref sSpoolsAssembly,
						ref sSpoolsBending, ref sSpoolsChecking, ref sSpoolsStartEnd, ref sSpoolsDrawingProps);

					dicSpoolsMat.Add(sOid, sSpoolsMat);
					dicSpoolsWeld.Add(sOid, sSpoolsWeld);
					dicSpoolsAssembly.Add(sOid, sSpoolsAssembly);
					dicSpoolsBending.Add(sOid, sSpoolsBending);
					dicSpoolsChecking.Add(sOid, sSpoolsChecking);
					dicSpoolsStartEnd.Add(sOid, sSpoolsStartEnd);
					dicSpoolsDrawingProps.Add(sOid, sSpoolsDrawingProps);
				}

			}
		}
	}
}
